package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import utils.Log4Test;

import java.util.concurrent.TimeUnit;

/**
 * Created by Sasha on 14.07.2014.
 */
public class TicketsPage extends HomePage {
    private static final String adultTicketsLocator = "//div[@class='adults_block passengers_wrapper']/div[@class='persons']/div[%d]";
    private static final String childTicketsLocator = "//div[@data-field='children']/div[@class='persons']/div[%d]";
    private static final String infantsTicketsLocator = "//div[@data-field='infants']/div[@class='persons']/div[%d]";
    private static final By searchButton = By.id("start_search");
    private static final By errorMess = By.xpath("//div[contains(@class,'error_popup')]//*[contains(text(),'Младенцев не может быть больше, чем взрослых')]");

    public TicketsPage(WebDriver driver){
        super(driver);
        URL = "http://rozetka.com.ua/travel/IEV/VIE/";
    }

    public void buyTickets(int adults, int children, int infants){
        WebDriverWait wait = new WebDriverWait(driver, 25);
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath(String.format(adultTicketsLocator,adults))));
        driver.findElement(By.xpath(String.format(adultTicketsLocator,adults))).click();
        driver.findElement(By.xpath(String.format(childTicketsLocator,children))).click();
        driver.findElement(By.xpath(String.format(infantsTicketsLocator,infants))).click();
        driver.findElement(searchButton).click();
    }

    public boolean isError() {
        return driver.findElements(errorMess).size() > 0;
    }
}
